{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# UCI Daphnet dataset (Freezing of gait for Parkinson's disease patients)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "from typing import List\n",
    "from pathlib import Path\n",
    "from config import data_raw_folder, data_processed_folder\n",
    "from timeeval import Datasets\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "plt.rcParams['figure.figsize'] = (20, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking for source datasets in /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset and\n",
      "saving processed datasets in /home/projects/akita/data/benchmark-data/data-processed\n"
     ]
    }
   ],
   "source": [
    "dataset_collection_name = \"Daphnet\"\n",
    "source_folder = Path(data_raw_folder) / \"UCI ML Repository/Daphnet/dataset\"\n",
    "target_folder = Path(data_processed_folder)\n",
    "\n",
    "print(f\"Looking for source datasets in {source_folder.absolute()} and\\nsaving processed datasets in {target_folder.absolute()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Directories /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet already exist\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S02R02.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R03.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S10R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S01R02.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S07R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S02R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S07R02.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S04R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S08R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R02.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S01R01.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R02.txt'),\n",
       " PosixPath('/home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R02.txt')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_type = \"unsupervised\"\n",
    "train_is_normal = False\n",
    "input_type = \"multivariate\"\n",
    "datetime_index = True\n",
    "dataset_type = \"real\"\n",
    "\n",
    "# create target directory\n",
    "dataset_subfolder = os.path.join(input_type, dataset_collection_name)\n",
    "target_subfolder = os.path.join(target_folder, dataset_subfolder)\n",
    "try:\n",
    "    os.makedirs(target_subfolder)\n",
    "    print(f\"Created directories {target_subfolder}\")\n",
    "except FileExistsError:\n",
    "    print(f\"Directories {target_subfolder} already exist\")\n",
    "    pass\n",
    "\n",
    "dm = Datasets(target_folder)\n",
    "experiments = [f for f in source_folder.iterdir()]\n",
    "experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = [\"timestamp\", \"ankle_horiz_fwd\", \"ankle_vert\", \"ankle_horiz_lateral\", \"leg_horiz_fwd\", \"leg_vert\", \"leg_horiz_lateral\",\n",
    "          \"trunk_horiz_fwd\", \"trunk_vert\", \"trunk_horiz_lateral\", \"is_anomaly\"]\n",
    "\n",
    "def transform_experiment_file(path: Path) -> List[pd.DataFrame]:\n",
    "    df = pd.read_csv(path, sep=\" \", header=None)\n",
    "    df.columns = columns\n",
    "    df[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"], unit=\"ms\")\n",
    "    # slice out experiments (0 annotation shows unrelated data points (preparation/briefing/...))\n",
    "    s_group = df[\"is_anomaly\"].isin([1, 2])\n",
    "    s_diff = s_group.shift(-1) - s_group\n",
    "\n",
    "    starts = (df[s_diff == 1].index + 1).values  # first point has annotation 0 --> index + 1\n",
    "    ends = df[s_diff == -1].index.values\n",
    "    \n",
    "    dfs = []\n",
    "    for start, end in zip(starts, ends):\n",
    "        df1 = df.iloc[start:end].copy()\n",
    "        df1[\"is_anomaly\"] = (df1[\"is_anomaly\"] == 2).astype(int)\n",
    "        dfs.append(df1)\n",
    "    return dfs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S02R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S02R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R03.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S03R03E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S10R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S10R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S10R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S10R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S01R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S01R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S09R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S09R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S09R01E2.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S09R01E3.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S09R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S09R01E4.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S07R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S07R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S06R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S06R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S06R01E2.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S03R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S03R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S02R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S02R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S07R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S07R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S04R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S04R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S04R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S04R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R01E2.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R01E3.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S08R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S08R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S08R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S08R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S08R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S08R01E2.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S08R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S08R01E3.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S06R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S06R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S06R02E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S01R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S01R01E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S01R01.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S01R01E1.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S03R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S03R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R02E0.test.csv\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/UCI ML Repository/Daphnet/dataset/S05R02.txt -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/Daphnet/S05R02E1.test.csv\n"
     ]
    }
   ],
   "source": [
    "for exp in experiments:\n",
    "    # transform file to get datasets\n",
    "    datasets = transform_experiment_file(exp)\n",
    "    for i, df in enumerate(datasets):\n",
    "        # get target filenames\n",
    "        experiment_name = os.path.splitext(exp.name)[0]\n",
    "        dataset_name = f\"{experiment_name}E{i}\"\n",
    "        filename = f\"{dataset_name}.test.csv\"\n",
    "        path = os.path.join(dataset_subfolder, filename)\n",
    "        target_filepath = os.path.join(target_subfolder, filename)\n",
    "\n",
    "        # calc length and save in file\n",
    "        dataset_length = len(df)\n",
    "        df.to_csv(target_filepath, index=False)\n",
    "        print(f\"Processed source dataset {exp} -> {target_filepath}\")\n",
    "\n",
    "        # save metadata\n",
    "        dm.add_dataset((dataset_collection_name, dataset_name),\n",
    "            train_path = None,\n",
    "            test_path = path,\n",
    "            dataset_type = dataset_type,\n",
    "            datetime_index = datetime_index,\n",
    "            split_at = None,\n",
    "            train_type = train_type,\n",
    "            train_is_normal = train_is_normal,\n",
    "            input_type = input_type,\n",
    "            dataset_length = dataset_length\n",
    "        )\n",
    "\n",
    "dm.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>train_path</th>\n",
       "      <th>test_path</th>\n",
       "      <th>dataset_type</th>\n",
       "      <th>datetime_index</th>\n",
       "      <th>split_at</th>\n",
       "      <th>train_type</th>\n",
       "      <th>train_is_normal</th>\n",
       "      <th>input_type</th>\n",
       "      <th>length</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>collection_name</th>\n",
       "      <th>dataset_name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"35\" valign=\"top\">Daphnet</th>\n",
       "      <th>S01R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S01R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>19200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S01R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S01R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>73600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S01R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S01R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>28800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S02R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S02R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>25600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S02R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S02R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>64960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S03R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S03R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>55040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S03R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S03R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>35840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S03R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S03R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>16640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S03R03E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S03R03E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>21120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S04R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S04R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>99840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S04R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S04R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>32640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>19840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>24320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R01E2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R01E2.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>14080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R01E3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R01E3.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>35840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S05R02E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S05R02E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>30080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S06R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S06R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>51200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S06R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S06R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>30720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S06R01E2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S06R01E2.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>25600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S06R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S06R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>7040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S06R02E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S06R02E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>12800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S07R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S07R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>74240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S07R02E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S07R02E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>28800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S08R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S08R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>14720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S08R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S08R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>10240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S08R01E2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S08R01E2.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>13440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S08R01E3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S08R01E3.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>10880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S09R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S09R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S09R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S09R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>10880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S09R01E2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S09R01E2.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>35200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S09R01E3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S09R01E3.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>46080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S09R01E4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S09R01E4.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S10R01E0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S10R01E0.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>51200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S10R01E1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/Daphnet/S10R01E1.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>91520</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             train_path  \\\n",
       "collection_name dataset_name              \n",
       "Daphnet         S01R01E0            NaN   \n",
       "                S01R01E1            NaN   \n",
       "                S01R02E0            NaN   \n",
       "                S02R01E0            NaN   \n",
       "                S02R02E0            NaN   \n",
       "                S03R01E0            NaN   \n",
       "                S03R01E1            NaN   \n",
       "                S03R02E0            NaN   \n",
       "                S03R03E0            NaN   \n",
       "                S04R01E0            NaN   \n",
       "                S04R01E1            NaN   \n",
       "                S05R01E0            NaN   \n",
       "                S05R01E1            NaN   \n",
       "                S05R01E2            NaN   \n",
       "                S05R01E3            NaN   \n",
       "                S05R02E0            NaN   \n",
       "                S05R02E1            NaN   \n",
       "                S06R01E0            NaN   \n",
       "                S06R01E1            NaN   \n",
       "                S06R01E2            NaN   \n",
       "                S06R02E0            NaN   \n",
       "                S06R02E1            NaN   \n",
       "                S07R01E0            NaN   \n",
       "                S07R02E0            NaN   \n",
       "                S08R01E0            NaN   \n",
       "                S08R01E1            NaN   \n",
       "                S08R01E2            NaN   \n",
       "                S08R01E3            NaN   \n",
       "                S09R01E0            NaN   \n",
       "                S09R01E1            NaN   \n",
       "                S09R01E2            NaN   \n",
       "                S09R01E3            NaN   \n",
       "                S09R01E4            NaN   \n",
       "                S10R01E0            NaN   \n",
       "                S10R01E1            NaN   \n",
       "\n",
       "                                                           test_path  \\\n",
       "collection_name dataset_name                                           \n",
       "Daphnet         S01R01E0      multivariate/Daphnet/S01R01E0.test.csv   \n",
       "                S01R01E1      multivariate/Daphnet/S01R01E1.test.csv   \n",
       "                S01R02E0      multivariate/Daphnet/S01R02E0.test.csv   \n",
       "                S02R01E0      multivariate/Daphnet/S02R01E0.test.csv   \n",
       "                S02R02E0      multivariate/Daphnet/S02R02E0.test.csv   \n",
       "                S03R01E0      multivariate/Daphnet/S03R01E0.test.csv   \n",
       "                S03R01E1      multivariate/Daphnet/S03R01E1.test.csv   \n",
       "                S03R02E0      multivariate/Daphnet/S03R02E0.test.csv   \n",
       "                S03R03E0      multivariate/Daphnet/S03R03E0.test.csv   \n",
       "                S04R01E0      multivariate/Daphnet/S04R01E0.test.csv   \n",
       "                S04R01E1      multivariate/Daphnet/S04R01E1.test.csv   \n",
       "                S05R01E0      multivariate/Daphnet/S05R01E0.test.csv   \n",
       "                S05R01E1      multivariate/Daphnet/S05R01E1.test.csv   \n",
       "                S05R01E2      multivariate/Daphnet/S05R01E2.test.csv   \n",
       "                S05R01E3      multivariate/Daphnet/S05R01E3.test.csv   \n",
       "                S05R02E0      multivariate/Daphnet/S05R02E0.test.csv   \n",
       "                S05R02E1      multivariate/Daphnet/S05R02E1.test.csv   \n",
       "                S06R01E0      multivariate/Daphnet/S06R01E0.test.csv   \n",
       "                S06R01E1      multivariate/Daphnet/S06R01E1.test.csv   \n",
       "                S06R01E2      multivariate/Daphnet/S06R01E2.test.csv   \n",
       "                S06R02E0      multivariate/Daphnet/S06R02E0.test.csv   \n",
       "                S06R02E1      multivariate/Daphnet/S06R02E1.test.csv   \n",
       "                S07R01E0      multivariate/Daphnet/S07R01E0.test.csv   \n",
       "                S07R02E0      multivariate/Daphnet/S07R02E0.test.csv   \n",
       "                S08R01E0      multivariate/Daphnet/S08R01E0.test.csv   \n",
       "                S08R01E1      multivariate/Daphnet/S08R01E1.test.csv   \n",
       "                S08R01E2      multivariate/Daphnet/S08R01E2.test.csv   \n",
       "                S08R01E3      multivariate/Daphnet/S08R01E3.test.csv   \n",
       "                S09R01E0      multivariate/Daphnet/S09R01E0.test.csv   \n",
       "                S09R01E1      multivariate/Daphnet/S09R01E1.test.csv   \n",
       "                S09R01E2      multivariate/Daphnet/S09R01E2.test.csv   \n",
       "                S09R01E3      multivariate/Daphnet/S09R01E3.test.csv   \n",
       "                S09R01E4      multivariate/Daphnet/S09R01E4.test.csv   \n",
       "                S10R01E0      multivariate/Daphnet/S10R01E0.test.csv   \n",
       "                S10R01E1      multivariate/Daphnet/S10R01E1.test.csv   \n",
       "\n",
       "                             dataset_type  datetime_index  split_at  \\\n",
       "collection_name dataset_name                                          \n",
       "Daphnet         S01R01E0             real            True       NaN   \n",
       "                S01R01E1             real            True       NaN   \n",
       "                S01R02E0             real            True       NaN   \n",
       "                S02R01E0             real            True       NaN   \n",
       "                S02R02E0             real            True       NaN   \n",
       "                S03R01E0             real            True       NaN   \n",
       "                S03R01E1             real            True       NaN   \n",
       "                S03R02E0             real            True       NaN   \n",
       "                S03R03E0             real            True       NaN   \n",
       "                S04R01E0             real            True       NaN   \n",
       "                S04R01E1             real            True       NaN   \n",
       "                S05R01E0             real            True       NaN   \n",
       "                S05R01E1             real            True       NaN   \n",
       "                S05R01E2             real            True       NaN   \n",
       "                S05R01E3             real            True       NaN   \n",
       "                S05R02E0             real            True       NaN   \n",
       "                S05R02E1             real            True       NaN   \n",
       "                S06R01E0             real            True       NaN   \n",
       "                S06R01E1             real            True       NaN   \n",
       "                S06R01E2             real            True       NaN   \n",
       "                S06R02E0             real            True       NaN   \n",
       "                S06R02E1             real            True       NaN   \n",
       "                S07R01E0             real            True       NaN   \n",
       "                S07R02E0             real            True       NaN   \n",
       "                S08R01E0             real            True       NaN   \n",
       "                S08R01E1             real            True       NaN   \n",
       "                S08R01E2             real            True       NaN   \n",
       "                S08R01E3             real            True       NaN   \n",
       "                S09R01E0             real            True       NaN   \n",
       "                S09R01E1             real            True       NaN   \n",
       "                S09R01E2             real            True       NaN   \n",
       "                S09R01E3             real            True       NaN   \n",
       "                S09R01E4             real            True       NaN   \n",
       "                S10R01E0             real            True       NaN   \n",
       "                S10R01E1             real            True       NaN   \n",
       "\n",
       "                                train_type  train_is_normal    input_type  \\\n",
       "collection_name dataset_name                                                \n",
       "Daphnet         S01R01E0      unsupervised            False  multivariate   \n",
       "                S01R01E1      unsupervised            False  multivariate   \n",
       "                S01R02E0      unsupervised            False  multivariate   \n",
       "                S02R01E0      unsupervised            False  multivariate   \n",
       "                S02R02E0      unsupervised            False  multivariate   \n",
       "                S03R01E0      unsupervised            False  multivariate   \n",
       "                S03R01E1      unsupervised            False  multivariate   \n",
       "                S03R02E0      unsupervised            False  multivariate   \n",
       "                S03R03E0      unsupervised            False  multivariate   \n",
       "                S04R01E0      unsupervised            False  multivariate   \n",
       "                S04R01E1      unsupervised            False  multivariate   \n",
       "                S05R01E0      unsupervised            False  multivariate   \n",
       "                S05R01E1      unsupervised            False  multivariate   \n",
       "                S05R01E2      unsupervised            False  multivariate   \n",
       "                S05R01E3      unsupervised            False  multivariate   \n",
       "                S05R02E0      unsupervised            False  multivariate   \n",
       "                S05R02E1      unsupervised            False  multivariate   \n",
       "                S06R01E0      unsupervised            False  multivariate   \n",
       "                S06R01E1      unsupervised            False  multivariate   \n",
       "                S06R01E2      unsupervised            False  multivariate   \n",
       "                S06R02E0      unsupervised            False  multivariate   \n",
       "                S06R02E1      unsupervised            False  multivariate   \n",
       "                S07R01E0      unsupervised            False  multivariate   \n",
       "                S07R02E0      unsupervised            False  multivariate   \n",
       "                S08R01E0      unsupervised            False  multivariate   \n",
       "                S08R01E1      unsupervised            False  multivariate   \n",
       "                S08R01E2      unsupervised            False  multivariate   \n",
       "                S08R01E3      unsupervised            False  multivariate   \n",
       "                S09R01E0      unsupervised            False  multivariate   \n",
       "                S09R01E1      unsupervised            False  multivariate   \n",
       "                S09R01E2      unsupervised            False  multivariate   \n",
       "                S09R01E3      unsupervised            False  multivariate   \n",
       "                S09R01E4      unsupervised            False  multivariate   \n",
       "                S10R01E0      unsupervised            False  multivariate   \n",
       "                S10R01E1      unsupervised            False  multivariate   \n",
       "\n",
       "                              length  \n",
       "collection_name dataset_name          \n",
       "Daphnet         S01R01E0       19200  \n",
       "                S01R01E1       73600  \n",
       "                S01R02E0       28800  \n",
       "                S02R01E0       25600  \n",
       "                S02R02E0       64960  \n",
       "                S03R01E0       55040  \n",
       "                S03R01E1       35840  \n",
       "                S03R02E0       16640  \n",
       "                S03R03E0       21120  \n",
       "                S04R01E0       99840  \n",
       "                S04R01E1       32640  \n",
       "                S05R01E0       19840  \n",
       "                S05R01E1       24320  \n",
       "                S05R01E2       14080  \n",
       "                S05R01E3        9600  \n",
       "                S05R02E0       35840  \n",
       "                S05R02E1       30080  \n",
       "                S06R01E0       51200  \n",
       "                S06R01E1       30720  \n",
       "                S06R01E2       25600  \n",
       "                S06R02E0        7040  \n",
       "                S06R02E1       12800  \n",
       "                S07R01E0       74240  \n",
       "                S07R02E0       28800  \n",
       "                S08R01E0       14720  \n",
       "                S08R01E1       10240  \n",
       "                S08R01E2       13440  \n",
       "                S08R01E3       10880  \n",
       "                S09R01E0        9600  \n",
       "                S09R01E1       10880  \n",
       "                S09R01E2       35200  \n",
       "                S09R01E3       46080  \n",
       "                S09R01E4        9600  \n",
       "                S10R01E0       51200  \n",
       "                S10R01E1       91520  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dm.refresh()\n",
    "dm.df().loc[(slice(dataset_collection_name,dataset_collection_name), slice(None))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Experimentation\n",
    "\n",
    "Annotations\n",
    "\n",
    "- `0`: not part of the experiment.\n",
    "  For instance the sensors are installed on the user or the user is performing activities unrelated to the experimental protocol, such as debriefing\n",
    "- `1`: experiment, no freeze (can be any of stand, walk, turn)\n",
    "- `2`: freeze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = [\"timestamp\", \"ankle_horiz_fwd\", \"ankle_vert\", \"ankle_horiz_lateral\", \"leg_horiz_fwd\", \"leg_vert\", \"leg_horiz_lateral\",\n",
    "          \"trunk_horiz_fwd\", \"trunk_vert\", \"trunk_horiz_lateral\", \"annotation\"]\n",
    "df1 = pd.read_csv(source_folder / \"S01R01.txt\", sep=' ', header=None)\n",
    "df1.columns = columns\n",
    "df1[\"timestamp\"] = pd.to_datetime(df1[\"timestamp\"], unit=\"ms\")\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = [c for c in columns if c not in [\"timestamp\", \"annotation\"]]\n",
    "df_plot = df1.set_index(\"timestamp\", drop=True)#.loc[\"1970-01-01 00:15:00\":\"1970-01-01 00:16:00\"]\n",
    "df_plot.plot(y=columns, figsize=(20,10))\n",
    "df_plot[\"annotation\"].plot(secondary_y=True)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_group = df1[\"annotation\"].isin([1, 2])\n",
    "s_diff = s_group.shift(-1) - s_group\n",
    "\n",
    "starts = (df1[s_diff == 1].index + 1).values\n",
    "ends = df1[s_diff == -1].index.values\n",
    "starts, ends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfs = [df1.iloc[start:end] for start, end in zip(starts, ends)]\n",
    "len(dfs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = [c for c in columns if c not in [\"timestamp\", \"annotation\"]]\n",
    "for df in dfs:\n",
    "    df = df.set_index(\"timestamp\", drop=True)\n",
    "    df.plot(y=columns, figsize=(20,10))\n",
    "    df[\"annotation\"].plot(secondary_y=True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "timeeval",
   "language": "python",
   "name": "timeeval"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
